package com.loveto.service;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.loveto.common.SoftKeyboardDetectorView;
import com.loveto.common.SoftKeyboardDetectorView.OnHiddenKeyboardListener;
import com.loveto.common.SoftKeyboardDetectorView.OnShownKeyboardListener;

public class OpenTournamentQuestionDirectActivity extends BaseActivity implements
View.OnClickListener {
	
	// 사용하는 변수들
	private Intent intentMoveToPage;
	private int presentInputRound;
	private boolean isShowKeyboard;
	private InputMethodManager mgr;
	private SoftKeyboardDetectorView softKeyboardDetector; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loveto_open_tournament_question_direct);
		
		Log.d("tag", "질문 직접 입력 시작~!");

		//변수 설정
		isShowKeyboard = false; //키보드 상태를 파악하기 위한
		mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		
		//전 엑티비티에서 정보 받아오기
		Intent intent = getIntent();
		presentInputRound = intent.getIntExtra("presentInputRound", 0);
		
		// 클릭 리스너 달기 // 버튼
		RelativeLayout btnSendQuestion = (RelativeLayout) findViewById(R.id.btn_send_question);
		btnSendQuestion.setOnClickListener(this);
		RelativeLayout btnGotoSelect = (RelativeLayout) findViewById(R.id.btn_goto_select_question);
		btnGotoSelect.setOnClickListener(this);
		RelativeLayout btnGogoBack = (RelativeLayout) findViewById(R.id.btn_goto_back);
		btnGogoBack.setOnClickListener(this);
		
		//키보드가 나왔는지 학인 처리
		softKeyboardDetector = new  SoftKeyboardDetectorView(this); //키보드가 나왔는지 확인하기 위한
		addContentView(softKeyboardDetector, new FrameLayout.LayoutParams(-1, -1));
		softKeyboardDetector.setOnShownKeyboard(new OnShownKeyboardListener() {
			@Override
			public void onShowSoftKeyboard() {
				isShowKeyboard = true;
				Log.d("tag", "키보드가 떴습니당~");
				Log.d("tag", "isShowKeyboard :" + isShowKeyboard);
			}
		});
		softKeyboardDetector.setOnHiddenKeyboard(new OnHiddenKeyboardListener() {
			
			@Override
			public void onHiddenSoftKeyboard() {
	        	reActiveButtons();
				Log.d("tag", "키보드가 내려갔습니당~");
			}
		});
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_send_question:
			if (!isShowKeyboard) {
				EditText vQuestion = (EditText) findViewById(R.id.editTextQusestion);
				String presentQuestionText = vQuestion.getText().toString();
				if (!("".equals(presentQuestionText))) {
					intentMoveToPage = new Intent(OpenTournamentQuestionDirectActivity.this,OpenTournamentMainActivity.class);
					SharedPreferences pref = getSharedPreferences("tournamentMakeInfo", Activity.MODE_PRIVATE);
					Editor editor = pref.edit();
					switch (presentInputRound) {
					case 1:
						editor.putString("lowCategoryNameRound01", "직접 질문");
						editor.putString("questionTextRound01", presentQuestionText);
						break;
					case 2:
						editor.putString("lowCategoryNameRound02", "직접 질문");
						editor.putString("questionTextRound02", presentQuestionText);
						break;
					case 3:
						editor.putString("lowCategoryNameRound03", "직접 질문");
						editor.putString("questionTextRound03", presentQuestionText);
						break;
					}
					editor.commit();
					startActivity(intentMoveToPage);
					finish();
				} else {
			        new AlertDialog.Builder(this)
			        .setMessage("질문을 입력하세요!")
			        .setNeutralButton("확인", null)
			        .show();
				}
			}
			break;
		case R.id.btn_goto_select_question:
			if (!isShowKeyboard) {
				intentMoveToPage = new Intent(OpenTournamentQuestionDirectActivity.this,OpenTournamentQuestionActivity.class);
				intentMoveToPage.putExtra("presentInputRound", presentInputRound);
				startActivity(intentMoveToPage);
				finish();
			}
			break;
		case R.id.btn_goto_back:
			finish();
			break;
		}
	}
	

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	    switch (ev.getAction()){
	        case MotionEvent.ACTION_DOWN:
	        	if (isShowKeyboard) {
	        		EditText vQuestion = (EditText) findViewById(R.id.editTextQusestion);
		        	mgr.hideSoftInputFromWindow(vQuestion.getWindowToken(), 0);
	        	}
	            break;
	        case MotionEvent.ACTION_UP:
	            break;
	    }
	    return super.dispatchTouchEvent(ev);
	}
	
	private void reActiveButtons() {
		new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
            	isShowKeyboard = false;
				Log.d("tag", "isShowKeyboard :" + isShowKeyboard);
            }
        }, 300);
	}
	
}
